<template>
  <div class="app-container">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['pollutant:water:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['pollutant:water:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['pollutant:water:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['pollutant:water:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="waterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="废水排放口ID" align="center" prop="wwoId" />-->
      <el-table-column label="排放口名称" align="center" prop="wwoName"/>
      <el-table-column label="排水去向类型" align="center" prop="wwoDirectionType"/>
      <el-table-column label="污水处理厂名称" align="center" prop="wwoPlantName"/>
      <!--      <el-table-column label="经度" align="center" prop="wwoLg" />
            <el-table-column label="纬度" align="center" prop="wwoLt" />
            <el-table-column label="照片" align="center" prop="wwoPicture" />
            <el-table-column label="浓度" align="center" prop="wwoConcentration" />
            <el-table-column label="总量" align="center" prop="wwoTotal" />-->
      <el-table-column label="最终受纳水体名称" align="center" prop="wwoAcceptorName"/>
      <!--      <el-table-column label="是否有在线监测设施(0,1)" align="center" prop="wwoHasOnlineFacility" />-->
      <el-table-column label="监督性监测报告" align="center" prop="wwoReport"/>
      <el-table-column label="环境监察记录" align="center" prop="wwoRecord"/>
      <!--      <el-table-column label="规范化排口(0,1)" align="center" prop="wwoHasNormalization" />
            <el-table-column label="规范化名称" align="center" prop="wwoNzName" />
            <el-table-column label="编号" align="center" prop="wwoNzCode" />
            <el-table-column label="导流槽长" align="center" prop="wwoLong" />
            <el-table-column label="导流槽宽" align="center" prop="wwoWith" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.wwoWith, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="导流槽高" align="center" prop="wwoHigh" />
            <el-table-column label="废水治理设施名称" align="center" prop="wtfName" />
            <el-table-column label="企业ID" align="center" prop="epId" />
            <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['pollutant:water:edit']"
          >修改
          </el-button>
          <el-button
            size="small"
            @click="handleDelete(scope.row)"
            v-hasPermi="['pollutant:water:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改废水排放口对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="60%">
      <el-form ref="form" :model="form" :rules="rules"  class="clearfix  m-f-a-i" label-width="110px"
               style="padding-left: 20px; padding-right: 20px">
        <div class="content-left" style="width: 45%">
          <el-form-item label="排放口名称" prop="wwoName">
            <el-input v-model="form.wwoName" placeholder="请输入排放口名称"/>
          </el-form-item>
          <el-form-item label="排水去向类型" prop="wwoDirectionType">
            <div style="width: 100%;">
              <el-select v-model="swdIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="swdChange">
                <el-option v-for="item in swd" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item label="污水处理厂名称" prop="wwoPlantName">
            <el-input v-model="form.wwoPlantName" placeholder="请输入污水处理厂名称"/>
          </el-form-item>
          <el-form-item label="经纬度">
            <el-row>
              <el-col>
                <div style="display: flex">
                  <div style="width: 38%">
                    <el-input v-model="form.wwoLg" placeholder="请输入经度"/>
                  </div>
                  <div style="width: 38%">
                    <el-input v-model="form.wwoLt" placeholder="请输入纬度"/>
                  </div>
                </div>
              </el-col>
            </el-row>
          </el-form-item>
          <el-form-item label="浓度" prop="wwoConcentration">
            <el-input v-model="form.wwoConcentration" placeholder="请输入浓度"/>
          </el-form-item>
          <el-form-item label="总量(吨)" prop="wwoTotal">
            <el-input v-model="form.wwoTotal" placeholder="请输入总量"/>
          </el-form-item>
        </div>
        <div class="content-left" style="float: right; width: 45%">
          <el-form-item label="最终受纳水体名称" prop="wwoAcceptorName">
            <el-input v-model="form.wwoAcceptorName" placeholder="请输入最终受纳水体名称"/>
          </el-form-item>
          <el-form-item label="是否在集中饮用水源保护区范围" prop="remark" label-width="220px">
            <el-select v-model="form.remark" placeholder="请选择" @change="changeOK">
              <el-option
                v-for="dict in typeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <div v-show="guolu">
          <el-form-item label="保护区级别" prop="updateBy">
            <el-input v-model="form.updateBy" placeholder="请输入保护级别"/>
          </el-form-item>
          </div>
          <el-form-item label="关联在线监测" prop="wwoHasOnlineFacility">
            <div style="width: 100%;">
              <el-select v-model="zxIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="zxChange">
                <el-option v-for="item in zx" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item label="监督性监测报告" prop="wwoReport">
            <FileUpload v-model="form.wwoReport" />
          </el-form-item>
          <el-form-item label="环境监察记录" prop="wwoRecord">
            <FileUpload v-model="form.wwoRecord" />
          </el-form-item>
          <el-form-item label="规范化排口" prop="wwoHasNormalization">
            <div style="width: 100%;">
              <el-select v-model="pkIndex" clearable placeholder="请选择" style="width: 100%;"
                         @change="pkChange">
                <el-option v-for="item in pk" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <div v-show="isShow">
            <el-form-item label="规范化名称" prop="wwoNzName">
              <el-input v-model="form.wwoNzName" placeholder="请输入规范化名称"/>
            </el-form-item>
            <el-form-item label="编号" prop="wwoNzCode">
              <el-input v-model="form.wwoNzCode" placeholder="请输入编号"/>
            </el-form-item>
            <el-form-item label="导流槽长" prop="wwoLong">
              <el-input v-model="form.wwoLong" placeholder="请输入导流槽长"/>
            </el-form-item>
            <el-form-item label="导流槽宽" prop="wwoWith">
              <el-date-picker clearable size="small"
                              v-model="form.wwoWith"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择导流槽宽">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="导流槽高" prop="wwoHigh">
              <el-input v-model="form.wwoHigh" placeholder="请输入导流槽高"/>
            </el-form-item>
          </div>
          <el-form-item label="照片" prop="wwoPicture">
            <ImageUpload v-model="form.wwoPicture" />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer tianjie_button">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addWater, delWater, exportWater, getWater, listWater, updateWater} from "@/api/pollutant/water";
import {InfoItem} from "@/api/enterprise/info";
import {isNotMUstTwoPoint,validatePhoneTwo,validateIdNo,validateEMail} from "@/api/validate/validate";


export default {
  name: "Water",
  data() {
    return {
      guolu:false,
      isShow: false,
      swd: [
        {
          value: '0',
          label: '自然流域(江河、湖、库)'
        },
        {
          value: '1',
          label: '城市下水道'
        },
        {
          value: '2',
          label: '城市污水处理厂'
        },
        {
          value: '3',
          label: '污灌农田'
        },
        {
          value: '4',
          label: '地渗或蒸发地'
        },
      ],
      zx: [
        {
          value: '0',
          label: '有'
        },
        {
          value: '1',
          label: '无'
        }
      ],
      zxIndex: '1',
      pk: [
        {
          value: '0',
          label: '是'
        },
        {
          value: '1',
          label: '否'
        }
      ],
      pkIndex: '1',
      swdIndex: '0',
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 废水排放口表格数据
      waterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        wwoName: null,
        wwoDirectionType: null,
        wwoPlantName: null,
        wwoLg: null,
        wwoLt: null,
        wwoPicture: null,
        wwoConcentration: null,
        wwoTotal: null,
        wwoAcceptorName: null,
        wwoHasOnlineFacility: null,
        wwoReport: null,
        wwoRecord: null,
        wwoHasNormalization: null,
        wwoNzName: null,
        wwoNzCode: null,
        wwoLong: null,
        wwoWith: null,
        wwoHigh: null,
        wtfName: null,
        epId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        wwoName: [
          {required: true, message: "排放口名称不能为空", trigger: "blur"}
        ],
        wwoTotal: [
          {validator:isNotMUstTwoPoint, trigger: "blur"}
        ],
      },
      typeOptions:[],
    };
  },
  created() {
    this.getDicts("common_whether").then(response => {
      this.typeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    changeOK(e){
      if(e == 0){
        this.guolu = true;
      }
      if(e == 1){
        this.guolu = false;
      }
    },
    swdChange(e) {
      this.form.wwoDirectionType = e;
    },
    zxChange(e) {
      this.form.exoHasOnlineFacility = e;
    },
    pkChange(e) {
      this.form.exoHasNormalization = e;
      if(e == 0){
        this.isShow = true;
      }

      if(e == 1){
        this.isShow = false;
      }
    },
    /** 查询废水排放口列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        this.queryParams.epId = res.data.epId;
        listWater(this.queryParams).then(response => {
          this.waterList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        wwoId: null,
        wwoName: null,
        wwoDirectionType: null,
        wwoPlantName: null,
        wwoLg: null,
        wwoLt: null,
        wwoPicture: null,
        wwoConcentration: null,
        wwoTotal: null,
        wwoAcceptorName: null,
        wwoHasOnlineFacility: null,
        wwoReport: null,
        wwoRecord: null,
        wwoHasNormalization: null,
        wwoNzName: null,
        wwoNzCode: null,
        wwoLong: null,
        wwoWith: null,
        wwoHigh: null,
        wtfName: null,
        epId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.wwoId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加废水排放口";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const wwoId = row.wwoId || this.ids
      getWater(wwoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改废水排放口";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.wwoId != null) {
            updateWater(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addWater(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const wwoIds = row.wwoId || this.ids;
      this.$confirm('是否确认删除废水排放口编号为"' + wwoIds + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delWater(wwoIds);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有废水排放口数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportWater(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
